// pages/topic_search/topic_search.js
import { request } from "../../request/index.js";
import { showModal, showToast, uploadFile, chooseImage, showLoading } from "../../util/AsyncWx.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topicList: [],
    goodsList: [],
    searchwords: '',
    flag: false,
    isNodata: '2'

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  searchInput(e) {
    this.setData({
      searchwords: e.detail.value
    })

  },
  chooseCatagory() {
    var that = this
    if (that.data.flag === false) {
      that.setData({
        flag: true

      })
    } else {
      that.setData({
        flag: false
      })
    }


  },
  async searchTopicSend() {
    var that = this
    var searchwords = that.data.searchwords
    showLoading({ title: "搜索中..." })
    const result = await request({ url: "/topic/getTopicByWords", data: { searchwords } })
    wx.hideLoading();
    if (result.length == 0) {
      that.setData({
        isNodata: '1',
        topicList: '',
        flag: false
      })
    } else {
      that.setData({
        topicList: result,
        goodsList: '',
        isNodata: '2',
        flag: false,
        i: '1'
      })
    }
  },
  async searchGoodsSend() {
    var that = this
    var searchwords = that.data.searchwords
    showLoading({ title: "搜索中..." })
    const result = await request({ url: "/goods/getGoodsByWords", data: { searchwords } })
    wx.hideLoading();
    if (result.length == 0) {
      that.setData({
        isNodata: '1',
        goodsList: '',
        flag: false
      })
    } else {
      that.setData({
        goodsList: result,
        topicList: '',
        isNodata: '2',
        flag: false,
        i: '1'
      })
    }
  },
  handleImagePreview: function (e) {
    var that = this
    var url = e.currentTarget.dataset.url
    wx.previewImage({
      current: url,
      urls: [url],
    });

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})